package jz.sats.config;

import jakarta.annotation.Resource;
import jz.sats.factory.LoveAppContextualQueryAugmenterFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.generation.augmentation.ContextualQueryAugmenter;
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: TODO
 * @Author: sats@jz
 * @Date: 2025/8/10 23:52
 **/
@Configuration
public class AdvisorConfiguration {
    @Resource
    private VectorStore pgVectorVectorStore;
    @Resource(name = "deepSeekChatModel")
    private ChatModel deepSeekChatModel;

    @Bean
    public Advisor retrievalAugmentationAdvisor(){
        return RetrievalAugmentationAdvisor.builder()
                .queryTransformers(RewriteQueryTransformer.builder()
                        .chatClientBuilder(ChatClient.builder(deepSeekChatModel).build().mutate())
                        .build())
                .documentRetriever(VectorStoreDocumentRetriever.builder()
                        .similarityThreshold(0.60)
                        .vectorStore(pgVectorVectorStore)
                        .topK(3)
                        .build())
                .queryAugmenter(LoveAppContextualQueryAugmenterFactory.createInstance())
                .build();
    }
}
